package com.bruce;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Regservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public Regservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 將輸入內容轉成 utf-8 
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String userid="";
		String nickname="";
		String upw1="";
		String upw2="";
		String email="";
		
		userid=request.getParameter("userid");
		nickname = request.getParameter("nickname");
		upw1=request.getParameter("upw1");
		upw2=request.getParameter("upw2");
		email=request.getParameter("email");
		
		
		Member m= new Member(userid,nickname,upw1,upw2,email); //建立物件 m
		HttpSession se=  request.getSession(); // 建立 session 物件
		se.setAttribute("m", m); //將物件m加入 session, 標籤也叫做m
		
		if (m.validate()) 
		{  	System.out.print("Regist Success!");
			request.getRequestDispatcher("listMember.jsp").forward(request, response);
		}
		else 
		{	System.out.print("Regist Fail");
			response.sendRedirect("reg.jsp");
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response); //再呼叫doGet()
	}

}
